//
//  Funcation.m
//  929爱限免项目
//
//  Created by qianfeng on 15/9/29.
//  Copyright (c) 2015年 LiuQing. All rights reserved.
//

#import "Funcation.h"

#import "AFHTTPRequestOperationManager.h"

#import "AFAppDotNetAPIClient.h"
@implementation Funcation

+(UIBarButtonItem *)setBarButtonItem:(NSString *)title andImageName:(NSString *)image callBack:(SEL)callback andTarget:(id)target
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 30);
    [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:target action:callback forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}


+(void)getWithURL:(NSString *)url success:(AFSuccessCall)success andFail:(AFFailureCall)fail
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        fail(error);
    }];

}
+(void)postWithURL:(NSString *)url paramters:(NSDictionary *)para success:(AFSuccessCall)success failure:(AFFailureCall)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

+(void)setNetIndicator:(BOOL)bl
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:bl];
}
+ (NSURLSessionDataTask *)sendWeiboWithBlock:(void (^)(NSArray *posts, NSError *error))block andContent:(NSString*)content andImage:(UIImage*)image {
    
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:ACCESS_TOKEN];
    
    NSDictionary *params = @{@"access_token":token,@"status":content};
    
    return [[AFAppDotNetAPIClient sharedClient] POST:@"2/statuses/upload.json" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:UIImagePNGRepresentation(image) name:@"pic" fileName:@"pic" mimeType:@"image/png"];
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"%@",responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

@end
